<%@page import="java.net.URLEncoder"%>
<%@page
	import="com.googlecode.contests_repo.core.config.ConfigurationManager"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<%@page import="com.googlecode.contests_repo.core.WebTools"%>
	<%@page
		import="com.googlecode.contests_repo.core.WebTools.ResourceType"%>
	<%@page import="com.googlecode.contests_repo.core.users.*"%>
	<%@page import="java.io.File"%>
	<%@include file='header.jsp'%>
	<%
		String path = request.getParameter("path"); // relative path 
		boolean canEdit = "1".equals(request.getParameter("edit"));
		String seriesRelativePath = ConfigurationManager.getInstance()
				.getSeriesRelativePath();

		if (path == null) {
			path = seriesRelativePath;
			response.sendRedirect("resource.jsp?path=" + seriesRelativePath);
		}
		if (request.getParameter("submitted") != null) {
			String result = WebTools.createEmptyResource(path,
					request.getParameter("new-resource"));
			if ("".equals(result)) {
				out.println("<div style='color:green'>Resource created successfully</div>");
			} else {
				out.println(String.format(
						"<div style='color:red'>%s</div>", result));
			}
		}

		String handler = WebTools.getResourceHandler(path);
		out.flush();
		RequestDispatcher dispatcher;
		if (handler != null) {
			dispatcher = request.getRequestDispatcher(handler);
			dispatcher.include(request, response);
		} else {
			response.sendRedirect(String.format("resource.jsp?path=%s",
					seriesRelativePath));
		}
		out.flush();

		// Edit link
		String userName = (String) session.getAttribute("username");
		String repoRoot = ConfigurationManager.getInstance().getRepoRoot();
		String resourcePath = repoRoot + File.separator + path;
		boolean hasUserEditRights = UserManager.hasAccessRight(userName,
				resourcePath, AccessRight.VIEW_FULL);
		if (!canEdit && hasUserEditRights) {
	%>
	<a href="resource.jsp?path=<%=path%>&edit=1">Edit</a>
	<%
		}
	%>

	<%
		// Create resource form
		ResourceType nextResource = WebTools.getResourceType(path)
				.getSuccessor();
		if (canEdit && hasUserEditRights) {
	%>
	<a href="resource.jsp?path=<%=path%>">Discard changes</a>
	<%
		}
		if (canEdit && hasUserEditRights && nextResource != null) {
	%>
	<form method="post">
		<input type="text" name="new-resource"><br> <input
			type="submit" name="submitted"
			value="Add <%=nextResource.getResourceTypeName()%>">
	</form>
	<br>
	<%
		}
		// Back button
		if (!seriesRelativePath.equals(path)) {
	%>
	<a href="resource.jsp?path=<%=WebTools.getRelativeParentPath(path)%>">Back</a>
	<%
		}
		out.println("<h2>Processing additional files</h2>");
	%>
	
	<a href="resource.jsp?path=<%= path + File.separator + "_public_files" %>"> Public extra resources </a>
</body>
</html>